package com.example.callmanagerandroid;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class ChoisePhoneNumberAdapter extends BaseAdapter{
	
	private List<String> liN = new ArrayList<String>();
	private Context context;
	

	public ChoisePhoneNumberAdapter( List<String> liN,
			Context context) {
		super();
		this.liN = liN;
		this.context = context;
	}

	@Override
	public int getCount() {
		return liN.size();
	}

	@Override
	public Object getItem(int position) {
		return liN.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View list;
		if (convertView == null) {
			list = new View(context);
			list = LayoutInflater.from(context).inflate(
					R.layout.choise_number, null);
		} else {
			list = (View) convertView;
		}
		
		list.setPadding(1, 1, 1, 1);
		TextView phone = (TextView) list.findViewById(R.id.textView1);
		phone.setText(liN.get(position));
		
		final SharedPreferences sharedpreferences = context.getSharedPreferences("sharethucoi", Context.MODE_PRIVATE);
		Boolean ischecked = sharedpreferences.getBoolean("key"+String.valueOf(position), false);
		
		CheckBox check = (CheckBox) list.findViewById(R.id.checkBox1);
		
		if(ischecked)
			check.setChecked(true);
		
		check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Editor editor = sharedpreferences.edit();
				editor.putBoolean("key"+String.valueOf(position), isChecked);
				editor.commit();
			}
		});
		
		
		return list;
	}

}
